<?php
namespace app\api\model;

/*客服模型*/
use app\api\model\CommonModel;
use think\Db;

class Config extends CommonModel
{
    private $table_name = 'config';
    public function __construct($args = [])
    {
       parent::__construct($args);
    }
    /**初始化参数
     * @param $args [client_id,token,enc_string,nickname]
     * @return array
     */
    private function default_param($args)
    {
        $default = [
            "cKey"     => "",
            "cValue" => "",
            "type"      => "text",
        ];
        return array_merge($default,$args);
    }

    //获取所有配置
    public function total()
    {
        $data = db($this->table_name)->select();
        $arr = array();
        if(!empty($data))
        {
            foreach($data as $v)
            {
                $arr[$v['cKey']] = $v['cValue'];
            }
        }
        //这里做些初始化配置的操作
        return $arr;
    }

    /**获取一个配置项
     * @param array $args key:[id,cKey], 中括号表示任选一个可以查询
     * @return bool
     */
    public function info($args)
    {

        if(empty($args)) return false;
        //参数合并
        $args = $this->default_param($args);
        //如果没有查询信息，返回空
        if((!isset($args['id']) || empty($args['id'] ))
            && empty($args['cKey'] )
        ) return false;

        //根据id,cKey其中一个查询
        if(!empty($args['id']))
        {
            $filter = ['id'=>$args['id']];
        }
        elseif(!empty($args['cKey']))
        {
            $filter = ['cKey'=>$args['cKey']];
        }
        if(empty($filter)) return false;

        //返回查询信息
        return  db($this->table_name)->where($filter)->find();
    }



}
